import json
import pymysql


class MySql:
    __conn = None
    __cursor = None

    def __init__(self):
        with open("setting.json", "r") as fp:
            mysql_cfg = json.load(fp)["mysql"]
        MySql.__conn = pymysql.connect(
            host=mysql_cfg["host"],
            user=mysql_cfg["user"],
            password=mysql_cfg["password"],
            database=mysql_cfg["database"],
            charset=mysql_cfg["charset"]
        )
        MySql.__cursor = MySql.__conn.cursor()

    def execute(self, sql):
        MySql.__cursor.execute(sql)
        MySql.__conn.commit()

        pass

    def __del__(self):
        if MySql.__conn is not None:
            MySql.__conn.close()
        if MySql.__cursor is not None:
            MySql.__cursor.close()
